
		function GALLERY(content,nav,time,startIndex,selectClass,callback){
				this.content=content;
				this.nav=nav;
				this.time=time;
				this.index=0;
				this.len=nav.length;
				this.end=false;
				this.inverval='';
				this.startIndex=startIndex;
				this.startFlag=true;
				this.selectClass=selectClass;
				this.callback=callback||this.callback;
				this.init();
			}
		GALLERY.prototype={
			constructor:GALLERY,
			init:function(){
				var context=this;
				var con=context.content;
				var nav=context.nav;
				context.lastIndex = 0;
				if(con.length<=1) return;
				for(var i=0,len=con.length;i<len;i++){
					$(con[i]).attr('index',i).hover(function(){
						//console.log('stop')
						context.stop();
					},function(){
						//console.log('start')
						context.start();
					});
					$(nav[i]).attr('index',i);
				}
				context.invoke(context.startIndex,1);
				context.start();
				context.navHover();
				//context.navClick();
			},
			prev:function(){
				var index=this.index;
				var context=this;
				var len=this.len;
				index<0?index=len-2:index--;
				context.invoke(index);
				context.stop();
				context.start();
			},
			invoke:function(index,flag){
				// alert('in')
				var context=this;
				context.callback.call(null,context,index,flag);
			},
			next:function(context){
				var context=this;
			    var index=context.index;
				var len=context.len;
				index>(len-2)?index=0:index++;
				context.invoke(index);
				context.stop();
				context.start();
			},
			start:function(){
				var context=this;
				context.inverval=function(){
					context.next();
				}
				if(context.startFlag){
					context.stop();
					context.myinterval=setInterval(context.inverval,context.time);
				}else{
					if(context.myinterval){
						context.stop();
					}
				}
			},
			stop:function(){
				var context=this;
				clearInterval(context.myinterval);
			},
			reset:function(){
				var context=this;
				context.stop();
				context.invoke(0);
			},
			confirmEnd:function(index){
				var context=this;
				if(index==context.len-1){
					context.end=true;
				}else{
					context.end=false;
				}
			},
			navHover:function(){
				var context=this;
				context.nav.hover(function(){
					context.stop();
					var index=$(this).attr('index');
					context.invoke(index);

				},function(){
					context.start();
				})
			},
			navClick:function(){
				var context=this;
				context.nav.click(function(){
					context.stop();
					var index=$(this).attr('index');
					context.invoke(index);
					//context.start();
				})
			},
			callback:function(context,index,flag){
                    var con=context.content;
                    con.stop(true,true).eq(context.lastIndex).fadeOut(200).end().eq(index).fadeIn(400);
                    var nav=context.nav;
                    var $cla=context.selectClass;
                    nav.removeClass($cla).eq(index).addClass($cla);
                    context.lastIndex = index;
                    context.index=index;
       			}
			};
(function(){
   var g = new GALLERY($('div.carousel_box li'),$('div.carousel_control a'),5000,0,'cur');
   if($('div.carousel_box li').length > 1){
         $('div.carousel_box a.btn_prev').on('click',function(){

             g.prev();
             return false;
         });
         $('div.carousel_box a.btn_next').on('click',function(){
             g.next();
             return false;
         });
       } else {
         $('div.carousel_box li').eq(0).show();
       }


   var MYSCROLL = {
       interval:null,
       per:3,//每行有多少个
       scrollPer:1,//每次滚动多少个
       intervalTime:5000,//自动滚动间隔时间
       animateTime:400,//滚动的动画时间
       wrap:'div.product_list',//整个滚动的包含的div
       nextBtn:'#content a.btn_next',
       prevBtn:'#content a.btn_prev',
       max:0,
       init:function(){
         var that = MYSCROLL;
         that['max'] = Math.ceil($(that['wrap']+' ul li').length/that['scrollPer']);
         if(that['max'] == 0){
            return ;
         }
         that['min'] = 0 ;
         that['start']();
         that['$ul'] = $(that['wrap']+' ul');
         that['$prevBtn'] = $(that['prevBtn']);
         that['$nextBtn'] = $(that['nextBtn']);
         that['$nextBtn'].data('page',0).on('click',function(){
           that.stop();
           that.next(this)
         });
         that['$prevBtn'].data("page",0).on('click',function(){
           that.stop();
           that.prev(this)}
           );
         $(that['wrap']).add(that['$nextBtn']).add(that['$prevBtn']).on('mouseenter',function(){
               that.stop();
         }).on('mouseleave',function(){
         	   // alert('im')
               that.start();
         })
       },
       prev:function(){
         var that = MYSCROLL;
         // that.stop();
         var page = that['$prevBtn'].data('page');
         var width = that['$ul'].find('li').eq(0).width()*that['scrollPer'];
         // console.log(page)
        if(page >= 0){
           page = that['per'] - that['scrollPer'] - that['max']+1;
         } else {
           page++;
         }
         $(that['wrap']+' ul').find('li').last().prependTo($(that['wrap']+' ul'));
         $(that['wrap']+' ul').css('left',-width).stop(true,true).animate({
           left: 0},that['animateTime'], function() {
           that['$nextBtn'].data('page',page);
           that['$prevBtn'].data('page',page);

           // $(that['wrap']+' ul').css({
           //     left:0
           // })
         });
       },
       next:function(){
         var that = MYSCROLL;
         var width = that['$ul'].find('li').eq(0).width()*that['scrollPer'];
         var page = $(that['nextBtn']).data('page');
         if(page <= that['per'] - that['scrollPer'] - that['max']+1){
           page = 0;
         } else {
           page--;
         }
         $(that['wrap']+' ul').animate({
           left:-width},
           that['animateTime'], function() {
           that['$nextBtn'].data('page',page);
           that['$prevBtn'].data('page',page);
           $(that['wrap']+' ul').find('li').eq(0).appendTo($(that['wrap']+' ul'));
           $(that['wrap']+' ul').css({
               left:0
           })
         });
       },
       stop:function(){
          var that = MYSCROLL;
          that['interval'] && clearInterval(that['interval']);
          that['interval'] = null;
       },
       start:function(){
         var that = MYSCROLL;
         that['interval'] && clearInterval(that['interval']);
         that['interval'] = setInterval(function(){
               that['next']();
         },that['intervalTime']);
       }
   }
   MYSCROLL.init();
  var ajax = function (url,callback,error){
    var deffered = $.ajax({
                url : url,
                dataType : 'jsonp',
                type : 'GET'
              ,statusCode: {
            404: function() {
              error();
            }
        }
      }).done(function(data){
                    callback && callback(data);
                }).fail(function(err){
                    error && error(err);
                });
             return deffered;
  };
   $(document).on('click', 'a.resource,a.source', function(){
            var re_type = $(this).attr('re_type');
           // console.log('in')
            post_data(re_type)
        });
   function getCookie(name){
        var val = getRealCookie(name);
        if($.trim(val) == ''){
            var vipcookie = getRealCookie('vipcookie');
            if($.trim(vipcookie)==''){
                return '';
            }
            var cookies = vipcookie.split('&');
            for(var i=0;i<cookies.length;i++){
                ary = cookies[i].split('=');
                if(ary.length>1 && ary[0] == name){
                    return decodeURIComponent(ary[1]);
                }
            }
            return undefined;
        }else{
            return $.trim(val);//如果cookie中有该值，优先使用该值
        }
    }
       function setCookie(name,value,hours,isBaseDomain){
        // console.log(value)
        value = value + '';
        if(isBaseDomain != undefined && isBaseDomain == 1){
            setRealCookie(name,value,hours,1);
        }else{
            var vipcookie = getRealCookie('vipcookie');
            if($.trim(value)==''){//删除cookie
                if(vipcookie!=''){
                    var check = getCookie(name);
                    if(check!=''){
                        var cookies = vipcookie.split('&');
                        var newcookie = new Array;
                        for(var i=0;i<cookies.length;i++){
                            ary = cookies[i].split('=');
                            if(ary.length>1 && ary[0] != name){
                                newcookie.push(cookies[i]);
                            }
                        }
                        vipcookie = newcookie.join('&');
                    }
                }
            }else{//添加cookie
                //删除原生cookie中的此值
                setRealCookie(name,'',0);
                if(vipcookie==''){
                    vipcookie = $.trim(name)+'='+encodeURIComponent(value);
                }else{
                    //check if has the same item , if so , replace it , otherwise add it.
                    var check = getCookie(name);
                    if(check!=''){
                        var cookies = vipcookie.split('&');
                        for(var i=0;i<cookies.length;i++){
                            ary = cookies[i].split('=');
                            if(ary.length>1 && ary[0] == name){
                                cookies[i] = name+'='+encodeURIComponent(value);
                                break;
                            }
                        }
                        vipcookie = cookies.join('&');
                    }else{
                        vipcookie = vipcookie+'&'+$.trim(name)+'='+encodeURIComponent(value);
                    }
                }
            }
            if(hours != undefined){
                setRealCookie('vipcookie',vipcookie,hours);
            }else{
                setRealCookie('vipcookie',vipcookie);
            }
        }
    }


    function setRealCookie(name,value,hours,isBaseDomain){
        //console.log(config['domain'])
        if(window.location.href.indexOf('jsq.xunlei.com')!=-1){
            domain = "xunlei.com";
        } else {
            domain = "xunlei.com";
        }
        if(arguments.length>2){
            var expireDate=new Date(new Date().getTime()+hours*3600000);
            if(isBaseDomain != undefined && isBaseDomain == 1){
                document.cookie = name + "=" + encodeURIComponent(value) + "; path=/; domain=xunlei.com; expires=" + expireDate.toGMTString() ;
            }else{
                document.cookie = name + "=" + encodeURIComponent(value) + "; path=/; domain=xunlei.com; expires=" + expireDate.toGMTString() ;
            }
        }else{
             document.cookie = name + "=" + encodeURIComponent(value) + "; path=/; domain="+domain;
        }

    }
  function S4()
    {
       return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
    }
    function NewGuid()
    {
       return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
    }




  function getRealCookie(name){
        return (document.cookie.match(new RegExp("(^"+name+"| "+name+")=([^;]*)"))==null)?undefined:decodeURIComponent(RegExp.$2);
    }
  function post_data(type){
         var userid = getCookie('userid');
         if(!userid){
           var userid = getCookie('ssid');
           if(!userid){
              var random = NewGuid();
              setCookie('ssid',random);
              userid = random;
           }
         }
         ajax('http://dypay.vip.xunlei.com/user/vipstat/?source='+type+'&ext='+userid+'&ext2=',function(){

         })
      }
   }
)()
